package com.miyake.server.servlet;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import com.miyake.server.DsImage;
import com.miyake.server.dsaccess.NodeManager;
import com.miyake.server.user.UserManager;

public class StoreImage {
	public StoreImage(InputStream is, String key, String name) {
	    DsImage image = new DsImage();

	    ByteArrayOutputStream baos = new ByteArrayOutputStream();
	    BufferedOutputStream bos = new BufferedOutputStream(baos);
        int len;           
        byte[] buffer = new byte[8192];

        try {
	        while ((len = is.read(buffer, 0, buffer.length)) != -1) {             
	        	baos.write(buffer, 0, len);
	        } 
	        baos.flush();
	        image.setBin(baos.toByteArray());
			bos.close();
			
			String[] str = key.split(",");
			if (str[0].equals("dataKey")) {
				NodeManager.getInstance().createImage(str[1], name, image, -1);
			}
			else if (str[0].equals("faceKey")) {
				UserManager.getInstance().addFaceImage(str[1], name, image);
			}
			
        }
        catch(Exception e) {	
        }
	}
}
